<?php
require_once __DIR__ . "/Base_model.php";

class Host_type_model extends Base_model
{
    public function __construct()
    {
        parent::__construct();
        $this->table = "type";
        $this->table1 = "type_sub";
        $this->table2 = "biaoqian";
        $this->table3 = "video";
        $this->table4 = "quality_video";
        $this->table5 = "user_material";
        $this->moren_type = 1;//默认分类id
        $this->moren_huati = 1;//默认话题id
    }

    /**
     * 查询分类
     */
    public function type($arr)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where("is_other !=", 999999999);
        //分页
        if (!empty($arr['page']) && !empty($arr['limit'])) {
            $pages = ($arr['page'] - 1) * $arr['limit'];
            $datas['total'] = $this->db->count_all_results("", false);
            $this->db->limit($arr['limit'], $pages);
        } else {
            $datas['total'] = $this->db->count_all_results("", false);
        }
        //
        $this->db->order_by('test_sort', "asc");
        $res = $this->db->get();
        $datas['row'] = $res->result_array();
        //        echo $this->db->last_query();
        return $datas;
    }


    /**
     * 所有话题分类
     * @param array $where
     * @param string $order
     * @return mixed
     */
    public function get_all($where=[],$order=''){
        $this->db->select('*');
        $this->db->from($this->table);
        if(!empty($where)){
            $this->db->where($where);
        }
        if(!empty($order)){
            $this->db->order_by($order);
        }else{
            $this->db->order_by('id desc');
        }
        $res = $this->db->get();
        $data = $res->result_array();
//        echo $this->db->last_query();exit;
        return $data;
    }

    /**
     * 查询分类
     */
    public function getOneType($id)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where("id", $id);
        $res = $this->db->get();
        $data = $res->row_array();
        return $data;
    }

    /**
     * 查询标签
     */
    public function biaoqian()
    {
        $this->db->select('*');
        $this->db->from($this->table2);
        //这是不加入已挑选视频
        $this->db->where("status", 1);
        $res = $this->db->get();
        $datas = $res->result_array();
        //        echo $this->db->last_query();
        return $datas;
    }

    /**
     * 发布分类
     */
    public function fabu()
    {
        //7.先查出所有分类的test字段名
        $this->db->select('id,test_name,test_sort,test_biaoqian,test_online');
        $this->db->from($this->table);
        $res = $this->db->get();
        $all = $res->result_array();
        //8.修改正式
        if (!empty($all)) {
            $length = count($all);
            foreach ($all as $k => $v) {
                $data['name'] = $v['test_name'];
                $data['sort'] = $v['test_sort'];
                $data['biaoqian'] = $v['test_biaoqian'];
                $data['online'] = $v['test_online'];
                $where['id'] = $v['id'];
                if ($length == $k + 1) {
                    $res = $this->db->update($this->table, $data, $where);
                } else {
                    $this->db->update($this->table, $data, $where);
                }
                //                $this->db->update($this->table,$data,$where);
            }
        } else {
            return true;
        }

        //1.先去查询分类表的数据，查出下线的数据
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where("online", 1);
        $this->db->order_by("add_time", "desc");
        $res = $this->db->get();
        $down = $res->result_array();
        //2.先去下线这个分类下的所有素材
        if (!empty($down)) {
            $da1 = [];//原始素材表
            $da2 = [];//精品视频表
            $da3 = [];//用户素材
            $da4 = [];//用户素材
            foreach ($down as $k => $v) {
                //2.1先去下线原始素材表
                $this->db->select('video_id');
                $this->db->from($this->table3);
                $this->db->where("video_join_type", $v['id']);
                $res = $this->db->get();
                $r1 = $res->result_array();
                if (!empty($r1)) {
                    $da1[] = $r1;
                }
                //2.2去下线精品素材表
                $this->db->select('id');
                $this->db->from($this->table4);
                $this->db->where("quality_join_type", $v['id']);
                $res = $this->db->get();
                $r2 = $res->result_array();
                if (!empty($r2)) {
                    $da2[] = $r2;
                }
                //2.3去下线用户素材表
                $this->db->select('id,join_type,join_type_sub');
                $this->db->from($this->table5);
                $this->db->where("join_type", $v['id']);
                $res = $this->db->get();
                $r3 = $res->result_array();
                if (!empty($r3)) {
                    $da3[] = $r3;
                }
                //2.4查询这个分类下的话题
                $this->db->select('id');
                $this->db->from($this->table1);
                $this->db->where("sub_type", $v['id']);
                $res = $this->db->get();
                $r4 = $res->result_array();
                if (!empty($r4)) {
                    $da4[] = $r4;
                }
            }
            //3.下线原始素材表
            if (!empty($da1)) {
                foreach ($da1 as $q1 => $w1) {
                    if (!empty($w1)) {
                        foreach ($w1 as $q11 => $w11) {
                            $qw1['video_type'] = 1;
                            $qw1['sorts'] = 999999999;
                            $qw1['video_join_type'] = $this->moren_type;
                            $qw1['video_join_sub'] = $this->moren_huati;
                            $this->update($this->table3, $qw1, ['video_id' => $w11['video_id']]);
                        }
                    }
                }
            }
            //4.下线精品视频素材表
            if (!empty($da2)) {
                foreach ($da2 as $q2 => $w2) {
                    if (!empty($w2)) {
                        foreach ($w2 as $q22 => $w22) {
                            $qw2['quality_join_type'] = $this->moren_type;
                            $qw2['quality_join_type_sub'] = $this->moren_huati;
                            $this->update($this->table4, $qw2, ['id' => $w22['id']]);
                        }
                    }
                }
            }
            //5.下线用户上传素材表
            if (!empty($da3)) {
                foreach ($da3 as $q3 => $w3) {
                    if (!empty($w3)) {
                        foreach ($w3 as $q33 => $w33) {
                            $qw3['join_type'] = $this->moren_type;
                            $qw3['join_type_sub'] = $this->moren_huati;
                            $qw3['old_join_type_sub'] = $w33['join_type_sub'];
                            $qw3['old_join_type'] = $w33['join_type'];
                            $qw3['examine'] = 1;
                            $qw3['quality_order'] = 999999999;
                            $qw3['type_sub_order'] = 999999999;
                            $this->update($this->table5, $qw3, ['id' => $w33['id']]);
                        }
                    }
                }
            }
            //6.去下线这个话题
            if (!empty($da4)) {
                foreach ($da4 as $q4 => $w4) {
                    if (!empty($w4)) {
                        foreach ($w4 as $q44 => $w44) {
                            $qw4['sub_type'] = $this->moren_type;
                            $qw4['online'] = 1;
                            $qw4['sort'] = 999999999;
                            $this->update($this->table1, $qw4, ['id' => $w44['id']]);
                        }
                    }
                }
            }
        }
        return $all;
    }

    /**
     * 修改排序
     */

    public function update_sort($sort, $id, $old_sort)
    {
        //1.先去查询所有排序为1-100的
        $this->db->select("id,test_sort");
        $this->db->from($this->table);
        $this->db->where("test_sort>=", 1);
        $this->db->where("test_sort<=", 100);
        $this->db->order_by('test_sort', "asc");
        $res1 = $this->db->get();
        $data1 = $res1->result_array();
        if (!empty($data1)) {
            //4.如果排序值小于已排序个数，把小于这个排序值的放入一个数组，从一开始依次排，大于这个排序的，放入另一个数组，从这个排序之后一次排
            $sort1 = [];
            $sort2 = [];
            $data2 = [];
            //4.原有排序值大于这个排序值的放入$sort2，小于的放入$sort1
            foreach ($data1 as $k => $v) {
                if ($v['test_sort'] < $sort && $v['test_sort'] != $old_sort) {
                    $sort1[] = $v['test_sort'];
                } else if ($v['test_sort'] >= $sort && $v['test_sort'] != $old_sort) {
                    $sort2[] = $v['test_sort'];
                } else {
                    //5.删除$data1中的值，方便遍历，用$data2遍历就行
                    unset($data1[$k]);
                }
            }
            foreach ($data1 as $a => $b) {
                $data2[] = $b;
            }
            $g1 = $this->s1($sort1);
            $g2 = $this->s2($sort2, $sort);
            $g3 = array_merge($g1, $g2);
            $sort3 = array_merge($sort1, $sort2);

            ///////////
            if (empty(array_diff_assoc($g3, $sort3))) {
                $res = $this->update($this->table, ['test_sort' => $sort], ['id' => $id]);
                if ($res) {
                    return true;
                } else {
                    return false;
                }

            } else {
                //先去把以前的排序值从1开始依次排序
                foreach ($data2 as $kk => $vv) {
                    $this->update($this->table, ['test_sort' => $g3[$kk]], ['id' => $vv['id']]);
                }
                //再去修改这个要排序的值
                $res = $this->update($this->table, ['test_sort' => $sort], ['id' => $id]);
                if ($res) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            $res = $this->update($this->table, ['test_sort' => $sort], ['id' => $id]);
            if ($res) {
                return true;
            } else {
                return false;
            }
        }


    }

    public function s1($sort)
    {
        if (!empty($sort)) {
            $sort[0] = 1;
            $len = count($sort);
            for ($i = 1; $i < $len; $i++) {
                $sort[$i] = $sort[$i - 1] + 1;
            }
            return $sort;
        } else {
            return [];
        }
    }

    public function s2($sort1, $sort)
    {
        if (!empty($sort1)) {
            $sort1[0] = $sort + 1;
            $len = count($sort1);
            for ($i = 1; $i < $len; $i++) {
                $sort1[$i] = $sort1[$i - 1] + 1;
            }
            return $sort1;
        } else {
            return [];
        }
    }


    /**
     * 判断分类是否存在
     */
    public function one_save($where, $id)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where($where);
        $this->db->where('id!=', $id);
        $res = $this->db->get();
        $datas = $res->result_array();
        //        echo $this->db->last_query();
        return $datas;
    }

}